﻿
//填充客户端数据
//top是页面的独立内存，当刷新页面，top内存会重新加载（此时，留下一些数据缓存在客户端，对场景应用更有帮助）
(function ($, ibpm) {
    "use strict";
    var loadSate = {
        no: -1,  // 还未加载
        yes: 1,  // 已经加载成功
        ing: 0,  // 正在加载中
        fail: 2  // 加载失败
    };
    //异步数据池
    var clientAsyncData = {};
    //客户端内存池
    var clientData = {};

    ibpm.clientdata = {
        init: function (callback) {
            //主动初始化，客户端缓存
            clientAsyncData.dataItem.init();
            clientAsyncData.SysExInfo.init();            
            if (!!callback) {
                callback();
            }
        },
        getAsync: function (name, op) {
            var dfop = {
                key: '',    //Code
                code: '',   //CodeType
                callback: false,
            };
            $.extend(dfop, op || {});
            return clientAsyncData[name].get(dfop);
        },
        getAllAsync: function (name, op) {  //op = {code, callback}
            var dfop = {
                code: '',
                callback: false,
            };
            $.extend(dfop, op || {});
            return clientAsyncData[name].getAll(dfop);
        },
        getsAsync: function (name, op) {
            var dfop = {
                key: '',    //'value1,value2,value3'
                code: '',
                callback: false,
            };
            $.extend(dfop, op || {});
            return clientAsyncData[name].gets(dfop);
        },
        update: function (name) {
            clientAsyncData[name].update && clientAsyncData[name].update();
        }
    };

    /*******************使用时异步获取*******************/
    var storage = {
        get: function (name) {
            if (localStorage) { //HTML5本地存储Localstorage
                return JSON.parse(localStorage.getItem(name)) || {};
            }
            else {
                return clientData[name] || {};  //存储的是对象
            }
        },
        set: function (name, data) {
            if (localStorage) {
                localStorage.setItem(name, JSON.stringify(data));
            }
            else {
                clientData[name] = data;
            }
        }
    };
    /*******************缓存异步加载数据的安排***********************/

    // 数据字典
    clientAsyncData.dataItem = {
        states: loadSate.no,
        init: function () {
            if (clientAsyncData.dataItem.states == loadSate.no) {
                clientAsyncData.dataItem.states = loadSate.ing;
                //取“字典”数据--数据结构是顺表的
                ibpm.httpAsync('GET', top.$.rootUrl + '/WorkDone/GetDataItemMap', { ver: 'KV' }, function (data) {
                    if (!data) {
                        clientAsyncData.dataItem.states = loadSate.fail;
                    } else {
                        storage.set("dataItemData", data);
                        clientAsyncData.dataItem.states = loadSate.yes;
                    }
                });
            }
        },
        get: function (op) {
            clientAsyncData.dataItem.init();
            if (clientAsyncData.dataItem.states == loadSate.ing) {
                setTimeout(function () {
                    clientAsyncData.dataItem.get(op);
                }, 100);// 如果还在加载100ms后再检测
            }
            else {
                var data = storage.get("dataItemData") || {};
                if (!!op.code && op.code != '') {
                    //查询过滤
                    data = $.grep(data, function (o, i) { return o.code == op.code; });
                }

                var _item = clientAsyncData.dataItem.find(op.key, data || {});
                if (_item) {
                    top.ibpm.language.get(_item.text, function (text) {
                        _item.text = text;
                        op.callback(_item, op);
                    });
                }
                else {
                    op.callback({}, op);
                }
            }
        },
        getAll: function (op) {
            clientAsyncData.dataItem.init();
            if (clientAsyncData.dataItem.states == loadSate.ing) {
                setTimeout(function () {
                    clientAsyncData.dataItem.getAll(op);
                }, 100);// 如果还在加载100ms后再检测
            }
            else {
                var data = storage.get("dataItemData") || {};
                if (!!op.code && op.code != '') {
                    //www.cnblogs.com/puyongsong/p/5999410.html
                    data = $.grep(data, function (o, i) { return o.code == op.code; });
                }
                
                var res = {};
                $.each(data || {}, function (_index, _item) {
                    _item.text = top.ibpm.language.getSyn(_item.text);
                    res[_index] = _item;
                });

                if (isEmptyObject(res)) {
                    clientAsyncData.dataItem.states = loadSate.no;
                    clientAsyncData.dataItem.getAll(op);
                }
                else
                    op.callback(res, op);
            }
        },
        gets: function (op) {
            clientAsyncData.dataItem.init();
            if (clientAsyncData.dataItem.states == loadSate.ing) {
                setTimeout(function () {
                    clientAsyncData.dataItem.gets(op);
                }, 100);// 如果还在加载100ms后再检测
            }
            else {
                var data = storage.get("dataItemData") || {};
                if (!!op.code && op.code != '') {
                    data = $.grep(data, function (o, i) { return o.code == op.code; });
                }

                var keyList = op.key.split(',');
                var _text = []
                $.each(keyList, function (_index, _item) {
                    var _item = clientAsyncData.dataItem.find(_item, data || {});
                    top.ibpm.language.get(_item.text, function (text) {
                        _text.push(text);
                    });
                });
                //op.callback(String(_text), op);
                op.callback(_text, op);
            }
        },
        find: function (key, data) {
            var res = {};
            for (var id in data) {
                if (data[id].value == key) {
                    res = data[id];
                    break;
                }
            }
            return res;
        },
        update: function () {
            clientAsyncData.dataItem.states = loadSate.no;
            clientAsyncData.dataItem.init();
        }
    };

    // ex注册数据
    clientAsyncData.SysExInfo = {
        states: loadSate.no,
        init: function () {
            if (clientAsyncData.SysExInfo.states == loadSate.no) {
                clientAsyncData.SysExInfo.states = loadSate.ing;
                //取“字典”数据--数据结构是顺表的
                ibpm.httpAsync('GET', top.$.rootUrl + '/WorkDone/GetExItemMap', { ver: 'EX' }, function (data) {
                    if (!data) {
                        clientAsyncData.SysExInfo.states = loadSate.fail;
                    } else {
                        storage.set("exinfoData", data);
                        clientAsyncData.SysExInfo.states = loadSate.yes;
                    }
                });
            }
        },
        get: function (op) {
            clientAsyncData.SysExInfo.init();
            if (clientAsyncData.SysExInfo.states == loadSate.ing) {
                setTimeout(function () {
                    clientAsyncData.SysExInfo.get(op);
                }, 100);// 如果还在加载100ms后再检测
            }
            else {
                var data = storage.get("exinfoData") || {};
                if (!!op.code && op.code != '') {
                    //查询过滤
                    data = $.grep(data, function (o, i) { return o.code == op.code; });
                }

                var _item = clientAsyncData.SysExInfo.find(op.key, data || {});
                if (_item) {
                    top.ibpm.language.get(_item.text, function (text) {
                        _item.text = text;
                        op.callback(_item, op);
                    });
                }
                else {
                    op.callback({}, op);
                }
            }
        },
        getAll: function (op) {
            clientAsyncData.SysExInfo.init();
            if (clientAsyncData.SysExInfo.states == loadSate.ing) {
                setTimeout(function () {
                    clientAsyncData.SysExInfo.getAll(op);
                }, 100);// 如果还在加载100ms后再检测
            }
            else {
                var data = storage.get("exinfoData") || {};
                if (!!op.code && op.code != '') {
                    data = $.grep(data, function (o, i) { return o.code == op.code; });
                }

                var res = {};
                $.each(data || {}, function (_index, _item) {
                    _item.text = top.ibpm.language.getSyn(_item.text);
                    res[_index] = _item;
                });

                if (isEmptyObject(res)) {
                    clientAsyncData.SysExInfo.states = loadSate.no;
                    clientAsyncData.SysExInfo.getAll(op);
                }
                else
                    op.callback(res, op);
            }
        },
        gets: function (op) {
            clientAsyncData.SysExInfo.init();
            if (clientAsyncData.SysExInfo.states == loadSate.ing) {
                setTimeout(function () {
                    clientAsyncData.SysExInfo.gets(op);
                }, 100);// 如果还在加载100ms后再检测
            }
            else {
                var data = storage.get("exinfoData") || {};
                if (!!op.code && op.code != '') {
                    data = $.grep(data, function (o, i) { return o.code == op.code; });
                }

                var keyList = op.key.split(',');
                var _text = []
                $.each(keyList, function (_index, _item) {
                    var _item = clientAsyncData.SysExInfo.find(_item, data || {});
                    top.ibpm.language.get(_item.text, function (text) {
                        _text.push(text);
                    });
                });
                //op.callback(String(_text), op);
                op.callback(_text, op);
            }
        },
        find: function (key, data) {
            var res = {};
            for (var id in data) {
                if (data[id].value == key) {
                    res = data[id];
                    break;
                }
            }
            return res;
        },
        update: function () {
            clientAsyncData.SysExInfo.states = loadSate.no;
            clientAsyncData.SysExInfo.init();
        }
    };

})(window.jQuery, top.ibpm);
